#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
对比有代理和无代理访问ScienceDirect的差异
"""

import requests
import json
from datetime import datetime

def test_access_comparison():
    """对比有代理和无代理的访问结果"""
    
    test_url = "https://www.sciencedirect.com"
    proxy_url = "http://36.147.78.166:80"
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
    }
    
    print("🔍 对比有代理和无代理访问ScienceDirect的差异")
    print("=" * 60)
    
    # 测试无代理访问
    print("\n📡 测试1: 无代理访问")
    print("-" * 30)
    
    try:
        response_no_proxy = requests.get(
            test_url, 
            headers=headers, 
            timeout=10,
            allow_redirects=True
        )
        
        print(f"✅ 状态码: {response_no_proxy.status_code}")
        print(f"📍 最终URL: {response_no_proxy.url}")
        print(f"📄 内容长度: {len(response_no_proxy.content)} bytes")
        
        # 检查响应头
        print("📋 关键响应头:")
        key_headers = ['server', 'cf-ray', 'cf-cache-status', 'set-cookie', 'x-frame-options']
        for header in key_headers:
            if header in response_no_proxy.headers:
                value = response_no_proxy.headers[header]
                if len(value) > 100:
                    value = value[:100] + "..."
                print(f"   {header}: {value}")
        
        # 检查是否被Cloudflare拦截
        if "cloudflare" in response_no_proxy.text.lower() and response_no_proxy.status_code != 200:
            print("⚠️  检测到Cloudflare拦截")
        elif "access denied" in response_no_proxy.text.lower():
            print("⚠️  检测到访问被拒绝")
        else:
            print("✅ 访问正常")
            
    except Exception as e:
        print(f"❌ 无代理访问失败: {e}")
        response_no_proxy = None
    
    # 测试代理访问
    print("\n📡 测试2: 代理访问")
    print("-" * 30)
    
    try:
        proxies = {
            'http': proxy_url,
            'https': proxy_url
        }
        
        response_with_proxy = requests.get(
            test_url, 
            headers=headers, 
            proxies=proxies,
            timeout=10,
            allow_redirects=True
        )
        
        print(f"✅ 状态码: {response_with_proxy.status_code}")
        print(f"📍 最终URL: {response_with_proxy.url}")
        print(f"📄 内容长度: {len(response_with_proxy.content)} bytes")
        
        # 检查响应头
        print("📋 关键响应头:")
        key_headers = ['server', 'cf-ray', 'cf-cache-status', 'set-cookie', 'x-frame-options']
        for header in key_headers:
            if header in response_with_proxy.headers:
                value = response_with_proxy.headers[header]
                if len(value) > 100:
                    value = value[:100] + "..."
                print(f"   {header}: {value}")
        
        # 检查是否被拦截
        if response_with_proxy.status_code == 403:
            print("❌ 403 Forbidden - 代理IP被封禁")
        elif response_with_proxy.status_code == 503:
            print("❌ 503 Service Unavailable - 服务不可用")
        elif "cloudflare" in response_with_proxy.text.lower() and "challenge" in response_with_proxy.text.lower():
            print("⚠️  检测到Cloudflare人机验证")
        elif "access denied" in response_with_proxy.text.lower():
            print("❌ 检测到访问被拒绝")
        else:
            print("✅ 代理访问正常")
            
    except Exception as e:
        print(f"❌ 代理访问失败: {e}")
        response_with_proxy = None
    
    # 对比分析
    print("\n📊 对比分析")
    print("=" * 60)
    
    if response_no_proxy and response_with_proxy:
        print(f"状态码对比: 无代理({response_no_proxy.status_code}) vs 代理({response_with_proxy.status_code})")
        print(f"内容长度对比: 无代理({len(response_no_proxy.content)}) vs 代理({len(response_with_proxy.content)})")
        
        if response_no_proxy.status_code == 200 and response_with_proxy.status_code == 403:
            print("\n🎯 结论: 代理IP被ScienceDirect识别并封禁")
            print("💡 建议:")
            print("   1. 使用住宅代理而非数据中心代理")
            print("   2. 轮换多个代理IP")
            print("   3. 降低请求频率")
            print("   4. 考虑使用无代理模式（如果本地IP可用）")
    
    # 获取真实IP信息
    print(f"\n🌐 IP信息检查")
    print("-" * 30)
    
    try:
        # 检查无代理的IP
        ip_response = requests.get("http://httpbin.org/ip", timeout=5)
        if ip_response.status_code == 200:
            real_ip = ip_response.json().get('origin', 'Unknown')
            print(f"真实IP: {real_ip}")
    except:
        print("无法获取真实IP信息")
    
    try:
        # 检查代理的IP
        ip_response = requests.get("http://httpbin.org/ip", 
                                 proxies={'http': proxy_url, 'https': proxy_url}, 
                                 timeout=5)
        if ip_response.status_code == 200:
            proxy_ip = ip_response.json().get('origin', 'Unknown')
            print(f"代理IP: {proxy_ip}")
    except:
        print("无法通过代理获取IP信息")

if __name__ == "__main__":
    test_access_comparison()